package common.io;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.xml.sax.InputSource;

public class FileUtil {
	public static String readAsString(String filename) throws IOException{
		File f = new File(filename);
		BufferedReader fin = new BufferedReader(new FileReader(f));
		String content = "";
		String line = "";
		while((line = fin.readLine()) != null){
			content+=line;
		}
		fin.close();
		return content;
	}
	public static String readLines(String filename) throws IOException{
		File f = new File(filename);
		BufferedReader fin = new BufferedReader(new FileReader(f));
		String content = "";
		String line = "";
		while((line = fin.readLine()) != null){
			content+=line+"\n";
		}
		fin.close();
		return content;
	}	
	public static void writeFileText(String filename, String content) throws FileNotFoundException{
		File f = new File(filename);
		PrintWriter fout = new PrintWriter(new FileOutputStream(f));
		fout.print(content);
		fout.close();
	}
	public static int[][] readMatrix(String filename, int column, int row, String regex) throws IOException{
		int[][] matrix = new int[row][column];
		File f = new File(filename);
		BufferedReader fin = new BufferedReader(new FileReader(f));
		int i = 0;
		while(i < row){
			String line = fin.readLine();
			String[] data = line.split(regex);
			for(int j=0;j<column;j++)
				matrix[i][j] = Integer.parseInt(data[j]);
			i++;
		}
		fin.close();
		return matrix;
	}
	public static String[] readListFile(String foldername){
		File folder = new File(foldername);
		ArrayList<String> myList = new ArrayList<String>();
		for(File fileEntry : folder.listFiles()){
			myList.add(fileEntry.getName());
		}
		String[] list = new String[myList.size()];
		list = myList.toArray(list);
		return list;
	}
	public static String[] readListFile(String foldername, String filetype){
		File folder = new File(foldername);
		ArrayList<String> myList = new ArrayList<String>();
		for(File fileEntry : folder.listFiles()){
			if(fileEntry.getName().endsWith(filetype))
					myList.add(fileEntry.getName());
		}
		String[] list = new String[myList.size()];
		list = myList.toArray(list);
		return list;
	}
	public static Document DOMparserXML(String filename) throws Exception{
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder docBuilder = factory.newDocumentBuilder();
		Document doc = docBuilder.parse(new File(filename));
		return doc;
	}
	public static Document DOMparserXML(String filename, String charset) throws Exception{
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder docBuilder = factory.newDocumentBuilder();
		InputStream is = new FileInputStream(new File(filename));
		Reader reader = new InputStreamReader(is, charset);
		InputSource source = new InputSource(reader);
		Document doc = docBuilder.parse(source);
		return doc;
	}
	public static void DOMwriteXML(String filename, Document doc) throws TransformerFactoryConfigurationError, TransformerException{
		File f = new File(filename);
		Source source = new DOMSource(doc);
		StreamResult result = new StreamResult(f);
		Transformer trans = TransformerFactory.newInstance().newTransformer();
		trans.setOutputProperty(OutputKeys.INDENT, "yes");
		trans.transform(source, result);
	}
	
}
